/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rhdtp.demopf.control.admin;

import com.rhdtp.demopf.model.dao.ActorDAO;
import com.rhdtp.demopf.model.dto.Actor;
import com.rhdtp.demopf.security.UserSession;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;

/**
 *
 * @author rhdtp
 */
public class CatalogoActor implements Serializable {

    private static final Logger logger = Logger.getLogger(CatalogoActor.class);
    private Actor actor;
    private ActorDAO actorDAO;
    private UserSession userSession;
    private List<Actor> actorsFilter;

    public CatalogoActor() {
    }

    public void save() {

        if (actor.getId() != 0) {
            actorDAO.update(actor);
            msgInfo("Info.", "El Actor: " + actor.getNombre() + " ah sido actualizado correctamente.");
        } else {
            actor.setId(actorDAO.countId() + 1);
            actorDAO.save(actor);
            msgInfo("Info.", "El Actor: " + actor.getNombre() + " ah sido guardado correctamente.");
        }

        init();
    }

    public void edit(Actor ac) {
        actor = actorDAO.load(ac);
        RequestContext.getCurrentInstance().scrollTo("registro:formulario");
    }

    public void remove(Actor a) {
        if (actorDAO.delete(a)) {
            msgInfo("Info.", "El Actor: " + a.getNombre() + " a sido borrado correctamente.");
        } else {
            msgError("Error", "No se pudo borrar el Actor, esto puede ser porque tiene algún registro dependiente");
        }
    }

    public void resetForm() {
        init();
        RequestContext.getCurrentInstance().reset("registro:formulario");
    }

    @PostConstruct
    public void init() {
        actor = new Actor();
        actorsFilter = null;
    }

    public void msgInfo(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgError(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgWarning(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public List<Actor> getActors() {
        return actorDAO.list();
    }

    public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }

    public void setActorDAO(ActorDAO actorDAO) {
        this.actorDAO = actorDAO;
    }

    public UserSession getUserSession() {
        return userSession;
    }

    public void setUserSession(UserSession userSession) {
        this.userSession = userSession;
    }

    public List<Actor> getActorsFilter() {
        return actorsFilter;
    }

    public void setActorsFilter(List<Actor> actorsFilter) {
        this.actorsFilter = actorsFilter;
    }
}
